package com.i2863.receipt.controller;

import com.i2863.receipt.entity.Receipt;
import com.i2863.receipt.service.ActivityService;
import com.i2863.receipt.service.ReceiptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */
@Controller
@RequestMapping("/receipt")
public class ReceiptController {

    @Autowired
    private ReceiptService receiptService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    private static int PAGESIZE = 10;


    /**
     * 列表
     * @param modelMap
     * @param startTime 起始时间
     * @param endTime 截止时间
     * @param pageNum 页码
     * @param activityId 活动id
     * @param queryContent 查询关键字（发票号码）
     * @return
     */
    @RequestMapping("/getlist")
    public String getList(ModelMap modelMap,
                          @RequestParam(value = "startTime",required = false)String startTime,
                          @RequestParam(value = "endTime",required = false)String endTime,
                          @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "activityId",required = false)Integer activityId,
                          @RequestParam(value = "queryContent",required = false)String queryContent){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        //查询站点下所有的活动
//        List<Activity> activities = activityService.findListBySiteId(siteId);
        String startDate = "";
        String endDate = "";
        if(StringUtils.isNotBlank(startTime)){
            startDate = startTime+" 00:00:00";
        }
        if(StringUtils.isNotBlank(endTime)){
            endDate = endTime+" 23:59:59";
        }
//        startDate = startTime+" 00:00:00";//开始时间从00:00:00秒开始
//        endDate = endTime+" 23:59:59";//结束时间从23:59:59秒结束
        Page<Receipt> page = receiptService.queryByPage(startDate,endDate,siteId,PAGESIZE,pageNum,queryContent,activityId);
        List<Receipt> list = page.getContent();
        modelMap.put("list",list);
        modelMap.put("total",page.getTotalElements());
        modelMap.put("pageNum",pageNum);
        modelMap.put("startTime",startTime);
        modelMap.put("endTime",endTime);
        modelMap.put("activityId",activityId);
        modelMap.put("queryContent",queryContent);
        modelMap.put("activityId",activityId);
//        modelMap.put("activities",activities);
        modelMap.put("pageSize",PAGESIZE);
        return "receipt/list";
    }



}
